<template>
  <transition name="el-zoom-in-top">
    <div v-show="visible" class="video-preview-wrapper">
      <div class="top">
        <div class="video-name">
          {{ activeFileObj.fileName }}.{{ activeFileObj.extendName }}
          <span class="size">{{
            $file.calculateFileSize(activeFileObj.fileSize)
          }}</span>
        </div>
        <a
          class="download-link"
          :href="$file.getDownloadFilePath(activeFileObj)"
          target="_blank"
        ><i class="download-icon el-icon-download" title="下载" /></a>
        <i
          class="fold-icon"
          :class="isFoldVideoList ? 'el-icon-s-fold' : 'el-icon-s-unfold'"
          :title="isFoldVideoList ? '展开播放列表' : '折叠播放列表'"
          @click="isFoldVideoList = !isFoldVideoList"
        />
        <i
          class="close-icon el-icon-close"
          title="关闭"
          @click="handleClosePreview"
        />
      </div>
      <div class="bottom">
        <video-player
          v-if="visible"
          ref="VideoPlayer"
          class="video-player"
          :source="activeVideoSource"
        />
        <el-collapse-transition>
          <div v-show="!isFoldVideoList" class="video-list-wrapper">
            <div class="list-title">播放列表</div>
            <ul class="video-list">
              <li
                v-for="(item, index) in videoList"
                :key="index"
                class="video-item"
                :class="[activeIndex === index ? 'active' : '']"
                @click="activeIndex = index"
              >
                <span
                  class="name"
                >{{ item.fileName }}.{{ item.extendName }}</span>
                <span class="size">{{
                  $file.calculateFileSize(item.fileSize)
                }}</span>
              </li>
            </ul>
          </div>
        </el-collapse-transition>
      </div>
    </div>
  </transition>
</template>

<script>
import VideoPlayer from './VideoPlayer.vue'

export default {
  name: 'VideoPreview',
  components: {
    VideoPlayer
  },
  data() {
    return {
      visible: false, //  视频预览组件是否可见
      activeIndex: 0, //  当前打开的视频索引
      isFoldVideoList: false //  是否折叠右侧视频列表
    }
  },
  computed: {
    // 当前显示的文件信息
    activeFileObj() {
      const res = this.videoList.length ? this.videoList[this.activeIndex] : {}
      return res
    },
    // 当前播放的视频源信息
    activeVideoSource() {
      return {
        type: `video/${this.videoList[this.activeIndex].extendName}`, //  取值文件后缀；这里的种类支持很多种：基本视频格式、直播、流媒体等，具体可以参看 https://videojs.com/getting-started
        src: this.videoList[this.activeIndex].fileUrl // url 地址
      }
    },
    // 屏幕宽度
    screenWidth() {
      return this.$store.state.netFile.fileScreenWidth
    }
  },
  watch: {
    // 监听视频预览组件状态
    visible(newValue) {
      if (newValue) {
        this.activeIndex = this.defaultIndex
      }
    },
    screenWidth(newValue) {
      if (newValue <= 768) {
        this.isFoldVideoList = true
      }
    }
  },
  mounted() {
    if (this.screenWidth <= 768) {
      this.isFoldVideoList = true
    }
  },
  methods: {
    // 关闭视频预览
    handleClosePreview() {
      this.visible = false
      this.callback('cancel')
    }
  }
}
</script>

<style lang="scss" scoped>
.video-preview-wrapper {
  background: rgba(0, 0, 0, 0.75);
  position: fixed;
  right: 80px;
  top: 60px;
  width: 70%;
  height: 86%;
  z-index: 20;

  .top {
    margin-bottom: 8px;
    background: #000;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 48px;
    line-height: 48px;
    padding: 0 8px 0 16px;

    .video-name {
      flex: 1;
      color: #fff;

      .size {
        margin-left: 16px;
        color: #909399;
        font-size: 12px;
      }
    }

    .download-link {
      display: block;
      height: 24px;
      line-height: 24px;
    }

    .download-icon, .fold-icon, .close-icon {
      margin-left: 8px;
      width: 32px;
      cursor: pointer;
      font-size: 24px;
      color: #fff;

      &:hover {
        opacity: 0.6;
      }
    }
  }

  .bottom {
    height: calc(100vh - 160px);
    display: flex;
    justify-content: space-between;

    .video-player {
      flex: 1;
      width: 100%;
      height: 100%;
      object-fit: fill;
    }

    .video-list-wrapper {
      margin-left: 8px;
      background: #000;
      width: 280px;
      height: 100%;
      color: #fff;

      .list-title {
        border-bottom: 2px solid #606266;
        height: 40px;
        line-height: 40px;
        padding: 0 16px;
      }

      .video-list {
        padding: 8px 0;
        font-size: 12px;
        cursor: pointer;
        height: calc(100% - 42px);
        overflow: auto;
        list-style: none;

        // 修改滚动条下面的宽度
        &::-webkit-scrollbar {
          width: 8px;
        }
        // 修改滚动条的下面的样式
        &::-webkit-scrollbar-track {
          background-color: #EBEEF5;
          -webkit-border-radius: 2em;
          -moz-border-radius: 2em;
          border-radius: 2em;
        }
        // 修改滑块
        &::-webkit-scrollbar-thumb {
          background-color: #909399;
          -webkit-border-radius: 2em;
          -moz-border-radius: 2em;
          border-radius: 2em;
        }
        .video-item {
          padding: 8px 16px;
          display: flex;
          justify-content: space-between;

          &:hover {
            color:  #409EFF;
          }

          .name {
            flex: 1;
            word-break: break-all;
          }

          .size {
            margin-left: 8px;
            width: 56px;
            color: #909399;
            text-align: right;
          }
        }

        .video-item.active {
          background: #000;
          color:  #409EFF;
        }
      }
    }
  }
}
</style>
